package demo2;

import java.util.concurrent.TimeUnit;

public class Wait1 {

    public static void main(String[] args) {
        Object lock = new Object();
        Object obj = new Object();

        new Thread(()->{
            synchronized (obj){
                System.out.println(Thread.currentThread().getName()+" start exec");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                try {
                    obj.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+" end exec");
            }
        },"thread1").start();
        new Thread(()->{
            synchronized (obj){
                System.out.println(Thread.currentThread().getName()+" start exec");
                try {
                    TimeUnit.SECONDS.sleep(2);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(Thread.currentThread().getName()+" end exec");
                obj.notify();
            }
        },"thread2").start();
    }
}
